define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'go'], function ($, undefined, Backend, Table, Form, Go) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cc/workflow/index',
                    add_url: 'cc/workflow/add',
                    edit_url: 'cc/workflow/edit',
                    del_url: 'cc/workflow/del',
                    multi_url: 'cc/workflow/multi',
                    table: 'cc_workflow',
                }
            });

            var table = $("#table");

            var buttons = [];
            buttons.push({
                name: 'workflow',
                title: __('设计'),
                classname: 'btn btn-xs btn-warning btn-addtabs',
                icon: 'fa fa-laptop',
                url: 'cc/workflow/detail'
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        {
                            field: 'operate',
                            width: "100px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: buttons,
                            formatter: Table.api.formatter.operate
                        },
                        {field: 'name', title: __('Name')},
                        {field: 'luruman', title: __('Luruman'), operate:false},
                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        detail: function () {
            var G = go.GraphObject.make;
            myDiagram =
                G(go.Diagram, "myDiagramDiv",
                    {
                        "grid.visible": true,
                        "grid.gridCellSize": new go.Size(30, 30),
                        "draggingTool.isGridSnapEnabled": true,
                        "resizingTool.isGridSnapEnabled": true,
                        "rotatingTool.snapAngleMultiple": 90,
                        "rotatingTool.snapAngleEpsilon": 45,
                        "undoManager.isEnabled": true
                    });
            myDiagram.addDiagramListener("Modified", function(e) {
                var button = document.getElementById("SaveButton");
                if (button) button.disabled = !myDiagram.isModified;
                var idx = document.title.indexOf("*");
                if (myDiagram.isModified) {
                    if (idx < 0) document.title += "*";
                } else {
                    if (idx >= 0) document.title = document.title.substr(0, idx);
                }
            });

            myDiagram.nodeTemplateMap.add("Process",
                G(go.Node, "Auto",
                    { locationSpot: new go.Spot(0.5, 0.5), locationObjectName: "SHAPE",
                        resizable: true, resizeObjectName: "SHAPE" },
                    new go.Binding("location", "pos", go.Point.parse).makeTwoWay(go.Point.stringify),
                    G(go.Shape, "Cylinder1",
                        { name: "SHAPE",
                            strokeWidth: 2,
                            fill: G(go.Brush, "Linear",
                                { start: go.Spot.Left, end: go.Spot.Right,
                                    0: "gray", 0.5: "white", 1: "gray" }),
                            minSize: new go.Size(50, 50),
                            portId: "", fromSpot: go.Spot.AllSides, toSpot: go.Spot.AllSides
                        },
                        new go.Binding("desiredSize", "size", go.Size.parse).makeTwoWay(go.Size.stringify)),
                    G(go.TextBlock,
                        { alignment: go.Spot.Center, textAlign: "center", margin: 5,
                            editable: true },
                        new go.Binding("text").makeTwoWay())
                ));

            myDiagram.nodeTemplateMap.add("Valve",
                G(go.Node, "Vertical",
                    { locationSpot: new go.Spot(0.5, 1, 0, -21), locationObjectName: "SHAPE",
                        selectionObjectName: "SHAPE", rotatable: true },
                    new go.Binding("angle").makeTwoWay(),
                    new go.Binding("location", "pos", go.Point.parse).makeTwoWay(go.Point.stringify),
                    G(go.TextBlock,
                        { alignment: go.Spot.Center, textAlign: "center", margin: 5, editable: true },
                        new go.Binding("text").makeTwoWay(),
                        // keep the text upright, even when the whole node has been rotated upside down
                        new go.Binding("angle", "angle", function(a) { return a === 180 ? 180 : 0; }).ofObject()),
                    G(go.Shape,
                        { name: "SHAPE",
                            geometryString: "F1 M0 0 L40 20 40 0 0 20z M20 10 L20 30 M12 30 L28 30",
                            strokeWidth: 2,
                            fill: G(go.Brush, "Linear", { 0: "gray", 0.35: "white", 0.7: "gray" }),
                            portId: "", fromSpot: new go.Spot(1, 0.35), toSpot: new go.Spot(0, 0.35) })
                ));

            myDiagram.linkTemplate =
                G(go.Link,
                    { routing: go.Link.AvoidsNodes, curve: go.Link.JumpGap, corner: 10, reshapable: true, toShortLength: 7 },
                    new go.Binding("points").makeTwoWay(),
                    // mark each Shape to get the link geometry with isPanelMain: true
                    G(go.Shape, { isPanelMain: true, stroke: "black", strokeWidth: 5 }),
                    G(go.Shape, { isPanelMain: true, stroke: "gray", strokeWidth: 3 }),
                    G(go.Shape, { isPanelMain: true, stroke: "white", strokeWidth: 1, name: "PIPE", strokeDashArray: [10, 10] }),
                    G(go.Shape, { toArrow: "Triangle", fill: "black", stroke: null })
                );

            Controller.api.load();
            Controller.api.loop();  // animate some flow through the pipes
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            loop: function () {
                var diagram = myDiagram;
                setTimeout(function () {
                    var oldskips = diagram.skipsUndoManager;
                    diagram.skipsUndoManager = true;
                    diagram.links.each(function (link) {
                        var shape = link.findObject("PIPE");
                        var off = shape.strokeDashOffset - 2;
                        shape.strokeDashOffset = (off <= 0) ? 20 : off;
                    });
                    diagram.skipsUndoManager = oldskips;
                    Controller.api.loop();
                }, 100);
            },
            save: function () {
                document.getElementById("mySavedModel").value = myDiagram.model.toJson();
                myDiagram.isModified = false;
            },
            load: function () {
                //myDiagram.model = go.Model.fromJson(document.getElementById("mySavedModel").value);
                myDiagram.model = go.Model.fromJson(Statsdata.gModel);
            }
        }
    };
    return Controller;
});